package 简单;

import java.util.Collection;
import java.util.HashMap;

public class 字符串中的第一个唯一字符387 {

    public static void main(String[] args) {
        String s = "ngdvupdfafpemlerwdtpgffhcqntjapfxjibatgriwjacatrqnmrdwistnnrbpxkldwkiluovjxcxuwmbssdqpkknpljdrxgdoghmillwvrcbondj" +
                "lvffkbhaibdpwaqnpiaukfcmdgjbxrmqawwjnajuvhhfvrksrkfpabsxdgnwgjqfimjoqmjaiewnxstccwgwkalpmbhkenijdalcuxffbagoduductdjkjn" +
                "egiqtbosedbngiixdilbrkgkskrmineivskgcetagfppcvpvcewfwmqikkrslgrudahtpwmihusbfdnrkvqmnacnnjklclbrppqvcokmggtujrirkmruwwnjd" +
                "qfaeulhoiuwcfedvljguqthmrcpihavmwaccsaolttnbksukeebhhdkodjufldxamfxmfghnpornwfrgbvlkxckbdjsljhaojmrsuhideeuxbmrjjtovahxeqh" +
                "dlstrhglxdadlwlfvhsxmsvwixjdbjdnwuwhqtseajiaooejuecwsduhtccnqwlcdieexchgokvildojbiicdcxuguuvemjpkferuuckipodieseejvcxanibwhd" +
                "nlvttmhorqvxwauppdxlfaippfhqmhcadtwoxnvunxavafpwhoouuvkhjanohbgdpmnqeqfkijlewnohqoxrcatmolojcckmpnjclviqswuvabbdstbsfakoxulk" +
                "ndpmepltftvpcoghojkjfeegwwhtcetxaefshtklqtnfkqhdqvgbjlkfoslfrlxtlntogoxtnvpmkwpionauowqxteoucbotggcjdtcsvrueioqwbjoiuojvhmxw" +
                "pfrjkwrkfqcihhuefrhfxiixttsnrspnpbimvhajdxhwkgomjghgjdxjlfgsanoqjlsmbhbppgllihtuupvaaclfftdbwgtqxmpcexsdqglxikkfxsuttfllxpbu" +
                "kfomieqviodvghscsxegarsirtxtcprsmcqemsvvuonvjiofogaxjspwmorghmdeefrrdaflhfaurshxephbavmntuhlnnwrcvarfmbnlmdbowgrurjthurnfgrs" +
                "lutrodwocxrghdwniktcwvvvnvnhemiddhreapkqtrutdcdhaovssluqrkrcxwexuajlajvgwbsohippmmubatflnjmhgdmgvvlvpjhiubewtxnffkkibmqgilp" +
                "njeagawodtwpowpkmhfdkmcewtahjspcejrbjmvclnsfbsmtodtrvpcnlpfanlhueukhobirxgllqkqaoxrqgiktqwuljtjnwlpcohbaxucwtelqmewqgvuwe" +
                "onrocwgbwijxhocjbobdcmehquauqppprhktighvweedpqfpebrhtfmpurjiiswsomvqorxtbemsaojimjjpguxmjwscnwkeesdfcwhcbijlcsxvtpplvkwhv" +
                "ludodmvfkllnxpsuikwokwniddupwapkipdnjdwqewricgmethlcpialnxrekvepmkqjhinvqthjampitklirspevalmuxwlrfekxtveftwkjsthgrfwqoats" +
                "ijhwuoanrwapltvngnjeirdnejwmugavhdfouufbggeiqjaotuqdvadqehxeewtofjxfiosmtnurdonjsmousqhhqqajekmzjetkawxajkjemknvetlagrhrsu" +
                "ldprukdlspldedhwuqcooubmpxketphbhstljfegvkjobowhtuojrmjecrgilsrregrbefegsmefojwhhjxephkfufbgpxatkoikwlsclnrcmmwwjhwngtxofel" +
                "uabuijligfxhsgflvjxgcaftwqigqnxfnuplwbntwljkpvdqfgjqntvnodqxjutpouaaatepbpqeqhaikemjguvforcmlstibrfbtviexqfuvtveerweosokfr" +
                "nkwaorvjohrabtrajthliumxqpmfqvtlkebmtskglrrnoeowcbfojpjujcaftvjpjhuptsrxmhtcigwcqnhmkwlkdgdswtwukgpswbatqlqrhjjufhfnbikqeld" +
                "utocmmptduikmithilxauxrybqoxvfidtixvxicomfpujuhqr";
        System.out.println(firstUniqChar(s));
    }


    public static int firstUniqChar(String s) {
        int length = s.length();
        int[] arr = new int[26];
        for (int i = 0; i < arr.length; i++) {
            arr[i]=-1;
        }
        for (int i = 0; i < length; i++) {
            int index = s.charAt(i) - 97;
            if (arr[index]==-1){
                //第一次
                arr[index] = i;
            }else if (arr[index]==-2){
                //已经是第2次以后了
            }else {
                //第二次
                arr[index] = -2;
            }
        }
        for (int i : arr) {
            System.out.println(i);
        }
        System.out.println("--------遍历-----------");
        int min = -3;
        System.out.println(arr);
        for (int i = 0; i < arr.length; i++) {
            if (min==-3&&arr[i]!=-1&&arr[i]!=-2){
                //当min为初始值,并且当前值不为-1和-2
                System.out.println("赋予min初始值");
                min=arr[i];
            }else if (min!=-3&&arr[i]!=-1&&arr[i]!=-2&&arr[i]<min){
                //交换min
                min = arr[i];
            }
        }

        return min==-3?-1:min;
    }
}
